//
//  EFAMsgModel.m
//  efairy
//
//  Created by XiangDongMBP on 2017/7/31.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAMsgModel.h"
#import "EFADeviceModel.h"

@implementation EFAMsgIMItem
@end
@implementation EFAMsgIMModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"device_msglist" : [EFAMsgIMItem class]};
}
- (instancetype)init {
    self = [super init];
    if (self) {
        _total_rows = 0;
        _device_msglist = [NSMutableArray array];
    }
    return self;
}
- (instancetype)initWithIMItemArray:(NSArray<EFAMsgIMItem *> *)array {
    self = [super init];
    if (self) {
        _total_rows = array.count;
        _device_msglist = [NSMutableArray arrayWithArray:array];
    }
    return self;
}
- (BOOL)totalLoad {
    return (self.device_msglist.count >= self.total_rows);
}
- (NSArray<EFAMsgIMItem *> *)selectedMsgIMArray {
    NSMutableArray *mArray = [NSMutableArray array];
    [self.device_msglist enumerateObjectsUsingBlock:^(EFAMsgIMItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected) {
            [mArray addObject:obj];
        }
    }];
    return mArray;
}
- (NSArray<NSNumber *> *)selectedMsgIMIdArray {
    NSMutableArray *mArray = [NSMutableArray array];
    [self.device_msglist enumerateObjectsUsingBlock:^(EFAMsgIMItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected) {
            [mArray addObject:@(obj.efairydevicemsg_id)];
        }
    }];
    return mArray;
}
- (void)addMoreWithData:(EFAMsgIMModel *)temp {
    if (!temp) {
        return;
    }
    self.total_rows = temp.total_rows;
    [self.device_msglist addObjectsFromArray:temp.device_msglist];
}
- (void)insertMsgItem:(EFAMsgIMItem *)item {
    if (!item) {
        return;
    }
    self.total_rows++;
    if (self.device_msglist.count) {
        [self.device_msglist insertObject:item atIndex:0];
    } else {
        [self.device_msglist addObject:item];
    }
}
- (void)insertMsgItemArray:(NSArray<EFAMsgIMItem *> *)array {
    if (!array.count) {
        return;
    }
    self.total_rows += array.count;
    if (self.device_msglist && self.device_msglist.count) {
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, array.count)];
        [self.device_msglist insertObjects:array atIndexes:indexSet];
    } else {
        self.device_msglist = [NSMutableArray arrayWithArray:array];
    }
}


- (void)addMsgItem:(EFAMsgIMItem *)item {
    if (item != nil) {
        self.total_rows += 1;
        if (self.device_msglist ) {
           [self.device_msglist addObject:item];
        }
    }
  
}

- (BOOL)containsMsgWithMsgId:(NSInteger)msg_id {
    for (EFAMsgIMItem *item in self.device_msglist) {
        if (item.efairydevicemsg_id == msg_id) {
            return YES;
        }
    }
    return NO;
}
- (void)removeAll {
    self.total_rows = 0;
    [self.device_msglist removeAllObjects];
}
- (void)clearAllSelected {
    [self.device_msglist enumerateObjectsUsingBlock:^(EFAMsgIMItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSelected = NO;
    }];
}
//- (void)changeAllSelected {
//    
//}
- (void)changeToHandleWithArray:(NSArray<EFAMsgIMItem *> *)array {
    [array enumerateObjectsUsingBlock:^(EFAMsgIMItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.efairydevicemsg_ishandle = YES;
    }];
}
- (void)changeToHandleWithIdArray:(NSArray<NSNumber *> *)array {
    [self.device_msglist enumerateObjectsUsingBlock:^(EFAMsgIMItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([array containsObject:@(obj.efairydevicemsg_id)]) {
            obj.efairydevicemsg_ishandle = YES;
        }
    }];
}
@end

@implementation EFAMsgUnhandleModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"unhandlemsg_list" : [NSNumber class]};
}

- (void)removeAll {
    self.total_rows = 0;
    [self.unhandlemsg_list removeAllObjects];
}
@end

#pragma mark - 设备配置
@implementation EFADeviceCfgModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairydevicesetting_thv_list" : [EFADevRealTimeDataItem class]};
}
@end


#pragma mark - 设备版本信息
@implementation EFADevVersionItem
@end

@implementation EFADevVersionModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"version_list" : [EFADevVersionItem class]};
}
- (BOOL)totalLoad {
    return (self.version_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.version_list.count == 0;
}
- (void)addMoreWithData:(EFADevVersionModel *)temp {
    self.total_rows = temp.total_rows;
    [self.version_list addObjectsFromArray:temp.version_list];
}
@end


#pragma mark - 企业代码列表
@implementation EFACompanyCodeItem
@end

@implementation EFACompanyCodeModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"companycode_list" : [EFACompanyCodeItem class]};
}
- (BOOL)totalLoad {
    return (self.companycode_list.count >= self.total_rows);
}
- (void)addMoreWithData:(EFACompanyCodeModel *)temp {
    self.total_rows = temp.total_rows;
    [self.companycode_list addObjectsFromArray:temp.companycode_list];
}
@end


#pragma mark - 用途代码列表
@implementation EFAUsageCodeItem
@end

@implementation EFAUsageCodeModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"usagecode_list" : [EFAUsageCodeItem class]};
}
- (BOOL)totalLoad {
    return (self.usagecode_list.count >= self.total_rows);
}
- (void)addMoreWithData:(EFAUsageCodeModel *)temp {
    self.total_rows = temp.total_rows;
    [self.usagecode_list addObjectsFromArray:temp.usagecode_list];
}
@end

@implementation EFAMsgModel

@end
